package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;

import java.io.IOException;


import liumengbaio.lolzhangyoubao.Activity.Adapter.VideoSecondAdapter;
import liumengbaio.lolzhangyoubao.Activity.Fragment.FragmentVideo;
import liumengbaio.lolzhangyoubao.Activity.InFo.VideoEntity;
import liumengbaio.lolzhangyoubao.Activity.InFo.VideoSecondNewest;
import liumengbaio.lolzhangyoubao.R;

/**
 * 二级主界面
 * <p/>
 * Created by lanouhn on 16/8/25.
 */
public class VideoActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private VideoSecondNewest mVideoSecondNewest;
    private VideoSecondAdapter mVideoSecondAdapter;
    private VideoEntity mVideoEntity;
    private TextView tv, tv_one;
    private ImageView iv, download, back;
    private String id;
    private RelativeLayout rl, rl_body;
    private String url = "http://lol.zhangyoubao.com/apis/rest/ItemsService/videos?" +
            "catid=&i_=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w" +
            "=&deviceId=JfoZvarCyLfJIqs9SRFolapk21/wEw63JVMaX7Y3v5w=&t_=1472094106442&p_" +
            "=21671&v_=400901&a_=lol&pkg_=com.anzogame.lol&page=1&d_=android&osv_=19&cha=360Martket&u_" +
            "=&modle_=MX4&api=ItemsService.videos&time=1472094106&game=lol&secretId=AKIDz8krbsJ5y" +
            "KBZQpn74WFkmLPx3gnPhESA&secretVersion=v1.0&nonce=900019&secretSignature=/VkJsSHiX1MI1n3MURR2B3zP08M=&catwordid=";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        initView();
        initData();
        initEvent();
        getDataFromWeb();
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.video_activity_rv);
        tv = (TextView) findViewById(R.id.video_activity_tvTitle);
        iv = (ImageView) findViewById(R.id.video_activity_ivHead);
        tv_one = (TextView) findViewById(R.id.video_activity_tvContent);
        rl = (RelativeLayout) findViewById(R.id.video_activity_rl);
        rl_body = (RelativeLayout) findViewById(R.id.video_activity_rlbody);
        download = (ImageView) findViewById(R.id.video_activity_download);
        back = (ImageView) findViewById(R.id.video_activity_back);
    }

    private void initData() {
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        tv.setText(name);
        final String content = intent.getStringExtra("desc");
        tv_one.setText(content);
        String url = intent.getStringExtra("pic");
        Picasso.with(this).load(url).into(iv);
        id = intent.getStringExtra("id");

        rl.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Intent intent = new Intent(VideoActivity.this, VideoThird_headActivity.class);
                intent.putExtra("desc", content);
                startActivity(intent);
                return false;
            }
        });
    }


    private void initEvent() {
        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(VideoActivity.this, VideoThird_downloadActivity.class);
                startActivity(intent);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getDataFromWeb() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url + id).build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(VideoActivity.this, "连接失败！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        mVideoSecondNewest = gson.fromJson(result, VideoSecondNewest.class);
                        if (mVideoSecondNewest.getData().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(VideoActivity.this, "抱歉，没有得到数据！", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    mVideoSecondAdapter = new VideoSecondAdapter(mVideoSecondNewest, VideoActivity.this);
                    mRecyclerView.setAdapter(mVideoSecondAdapter);
                    LinearLayoutManager llm = new LinearLayoutManager(VideoActivity.this);
                    mRecyclerView.setLayoutManager(llm);
                    break;
            }
        }
    };
}
